package com.sykj.cloud.auth.service;


import com.sykj.cloud.common.core.constant.Constants;
import com.sykj.cloud.common.core.domain.R;
import com.sykj.cloud.common.core.exception.BaseException;
import com.sykj.cloud.common.core.utils.StringUtils;
import com.sykj.cloud.common.core.utils.ip.IpUtils;
import com.sykj.cloud.common.core.web.domain.AjaxResult;
import com.sykj.cloud.common.redis.service.RedisService;
import com.sykj.cloud.common.security.utils.SecurityUtils;
import com.sykj.cloud.system.api.RemoteLogService;
import com.sykj.cloud.system.api.RemoteMemberService;
import com.sykj.cloud.system.api.domain.Member;
import com.sykj.cloud.system.api.model.LoginMember;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.sykj.cloud.common.core.constant.Constants.MEMBER_LOGIN_ERROR_COUNT;

/**
 * 登录校验方法
 */
@Component
public class MemberLoginService {
    @Autowired
    private RemoteLogService remoteLogService;

    @Autowired
    private RemoteMemberService remoteMemberService;

    @Autowired
    private RedisService redisService;

    /**
     * 增加会员登录失败次数
     */
    void addIpMemberLoginErrorCount() {
        // 组装条件，查询登录失败的次数
        Object cacheObject = redisService.getCacheObject(MEMBER_LOGIN_ERROR_COUNT + "_" + IpUtils.getHostIp());
        if (cacheObject != null) {
            redisService.setCacheObject(MEMBER_LOGIN_ERROR_COUNT + "_" + IpUtils.getHostIp(), Integer.parseInt(cacheObject.toString()) + 1);
        }else {
            redisService.setCacheObject(MEMBER_LOGIN_ERROR_COUNT + "_" + IpUtils.getHostIp(), 1);
        }
    }

    /**
     * 清空会员登录失败次数
     */
    void removeIpMemberLoginErrorCount() {
        redisService.deleteObject(MEMBER_LOGIN_ERROR_COUNT + "_" + IpUtils.getHostIp());
    }

    /**
     * 登录
     */
    public LoginMember memberLogin(String loginName, String password) {
        // 会员登录名或密码为空 错误
        if (StringUtils.isAnyBlank(loginName, password)) {
            remoteLogService.saveLogininfor(loginName, Constants.LOGIN_FAIL, "会员登录名密码必须填写");
            addIpMemberLoginErrorCount();
            throw new BaseException("会员登录名密码必须填写");
        }
        // 查询会员信息
        R<LoginMember> memberResult = remoteMemberService.getMemberInfo(loginName);
        if (R.FAIL == memberResult.getCode()) {
            remoteLogService.saveLogininfor(loginName, Constants.LOGIN_FAIL, memberResult.getMsg());
            addIpMemberLoginErrorCount();
            throw new BaseException(memberResult.getMsg());
        }
        LoginMember loginMember = memberResult.getData();
        if (StringUtils.isNull(memberResult) || StringUtils.isNull(memberResult.getData())) {
            remoteLogService.saveLogininfor(loginName, Constants.LOGIN_FAIL, "登录会员不存在");
            addIpMemberLoginErrorCount();
            throw new BaseException("登录会员：" + loginName + " 不存在");
        }
        Member member = loginMember.getMember();
        if (member.getDeleted()==1) {
            remoteLogService.saveLogininfor(loginName, Constants.LOGIN_FAIL, "对不起，您的账号已被删除");
            addIpMemberLoginErrorCount();
            throw new BaseException("对不起，您的账号：" + loginName + " 已被删除");
        }
        if (member.getLock()==1) {
            remoteLogService.saveLogininfor(loginName, Constants.LOGIN_FAIL, "会员已停用，请联系管理员");
            addIpMemberLoginErrorCount();
            throw new BaseException("对不起，您的账号：" + loginName + " 已停用");
        }
        if (!SecurityUtils.matchesPassword(password, member.getPassword())) {
            remoteLogService.saveLogininfor(loginName, Constants.LOGIN_FAIL, "会员密码错误");
            addIpMemberLoginErrorCount();
            throw new BaseException("会员不存在/密码错误");
        }
        remoteLogService.saveLogininfor(loginName, Constants.LOGIN_SUCCESS, "登录成功");
        // 删除 登录错误次数
        removeIpMemberLoginErrorCount();
        return loginMember;
    }

    public AjaxResult register(Member member) {
        return remoteMemberService.addHttp(member);
    }

    public void memberLogout(String loginName) {
        remoteLogService.saveLogininfor(loginName, Constants.LOGOUT, "退出成功");
    }
}
